05 / 12

What is the difference between 'IS-A' (Inheritance) and 'HAS-A' (Composition)?

Difficulty: 2/10

IS-A vs HAS-A

IS-A represents specialization and is normally modeled through inheritance. HAS-A represents ownership, collaboration or composition, where one object contains or uses another object. For example, a Car IS-A Vehicle, while a Car HAS-A Engine. In architecture, HAS-A relationships are generally more flexible because components can be replaced independently and the owning class does not need to inherit the implementation details of its dependency.

javascript
  1. 1

    IS-A: Represents specialization or substitutability.

  2. 2

    HAS-A: Represents composition, ownership or collaboration.

  3. 3

    Inheritance creates a stronger coupling between types.

  4. 4

    Composition allows dependencies to be replaced or configured.

  5. 5

    A good design distinguishes domain specialization from implementation reuse.

Follow-up Questions

  • Why is composition more flexible?
  • Can an object have multiple HAS-A relationships?
  • How does dependency injection relate to composition?
  • Give a real-world example where inheritance is inappropriate.
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.